<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业类型" prop="entType">
        <!-- <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" /> -->

        <el-select v-model="queryParams.entType" filterable clearable>
          <el-option label="政企单位" value="1"></el-option>
          <el-option label="教育医疗" value="2"></el-option>
          <el-option label="餐饮娱乐" value="3"></el-option>
          <el-option label="民用建筑" value="4"></el-option>
          <el-option label="商业综合体" value="5"></el-option>
          <el-option label="工业类" value="6"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="姓名" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <!-- <el-form-item label="创建人" prop="createUser">
          <el-input
            v-model="queryParams.createUser"
            placeholder="请输入创建人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="修改人" prop="updateUser">
          <el-input
            v-model="queryParams.updateUser"
            placeholder="请输入修改人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:template:add']"
          >新增</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:template:edit']"
          >修改</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:template:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:template:export']"
          >导出</el-button>
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="templateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <!-- <el-table-column label="标题" align="center" prop="title" /> -->
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="企业类型" align="center" prop="entTypeName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="queryInfo(scope.row)"
            v-hasPermi="['system:template:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 2)"
            >跟进</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            icon="el-icon-delete"
            @click="handleUpdate(scope.row, 3)"
            >作废</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷标题模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="姓名：" prop="title">
          <!-- <el-radio-group v-model="form.ifCurrentlyEnabled">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="2">不启用</el-radio>
            </el-radio-group> -->
          <div>{{ detailInfo.userName }}</div>
        </el-form-item>
        <el-form-item label="联系方式：" prop="title">
          <div>{{ detailInfo.phone }}</div>
        </el-form-item>
        <el-form-item label="企业类型：" prop="title">
          <div>{{ detailInfo.entTypeName }}</div>
        </el-form-item>

        <el-form-item label="创建时间：" prop="title">
          <div>{{ detailInfo.createTime }}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTitles, updateTitle } from "@/api/wb/wbApply";
import { listData } from "@/api/system/dict/data";
export default {
  name: "WbApply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷标题模板表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userName: null,
        sex: null,
        phone: null,
        createUser: null,
        updateUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: false, message: "请输入电话", trigger: "blur" },
          { min: 0, max: 11, message: "长度不超过11", trigger: "blur" },
        ],
        sex: [
          { required: false, message: "请输入性别", trigger: "blur" },
          { min: 1, max: 1, message: "长度不超过1", trigger: "blur" },
        ],
      },
      dataList: [],
      detailInfo: {},
    };
  },
  created() {
    this.getData();
  },
  methods: {
    /** 查询字典数据列表 */
    getData() {
      this.loading = true;
      listData({
        dictType: "ent_type",
      }).then((response) => {
        this.dataList = response.rows;
        this.getList();
      });
    },
    /** 查询问卷标题模板列表 */
    getList() {
      this.loading = true;
      listTitles(this.queryParams).then((response) => {
        this.templateList = response.rows;
        this.templateList.forEach((element) => {
          this.dataList.forEach((item) => {
            if (element.entType == item.dictValue * 1) {
              element.entTypeName = item.dictLabel;
            }
          });
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    queryInfo(row) {
      this.open = true;
      this.title = "详情";
      this.detailInfo = { ...row };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        userName: null,
        sex: null,
        phone: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null,
        ifCurrentlyEnabled: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问卷标题模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      let obj = {
        id: row.id,
        auditType: type,
      };
      updateTitle(obj).then((response) => {
        if (response.code == 200) {
          this.getList();
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(ids);
      this.$modal
        .confirm('是否确认删除问卷标题模板编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTemplate(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/template/export",
        {
          ...this.queryParams,
        },
        `template_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
